package flc.ast.activity;

import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.bean.ResumeBean;
import flc.ast.databinding.ActivityCertificateBinding;
import flc.ast.dialog.TitleRenameDialog;
import flc.ast.utils.ResumeDataUtils;

public class CertificateActivity extends BaseAc<ActivityCertificateBinding> {

    public static int position;
    public static int modulePos;
    private ResumeBean resumeBean;
    private ResumeBean.Module module;
    private ResumeBean.CertificateData certificateData;


    @Override
    protected int onCreate() {
        return R.layout.activity_certificate;
    }

    @Override
    protected void initView() {
        mDataBinding.ivCertificateBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvSave.setOnClickListener(this);
        mDataBinding.tvTitle.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        int id = v.getId();
        if (id == R.id.tvSave) {//保存
            save();
        } else if (id == R.id.tvTitle) {//标题
            showRenameDialog();
        }
    }

    @Override
    protected void initData() {
        resumeBean = ResumeDataUtils.getResumeList().get(position);
        module = resumeBean.getResumeData().get(modulePos);
        mDataBinding.tvTitle.setText(module.getAlias());
        if (module.getData() != null) {
            certificateData = GsonUtils.fromJson(module.getData().toString(), ResumeBean.CertificateData.class);;
            String detail = "";
            certificateData.setDetailList(GsonUtils.fromJson(certificateData.getDetailList().toString(), new TypeToken<List<ResumeBean.DetailItem>>() {
            }.getType()));
            for (ResumeBean.DetailItem detailItem : certificateData.getDetailList()) {
                detail += detailItem.getText() + "\n";
            }
            mDataBinding.etHonorary.setText(detail);
            String certificate = "";
            certificateData.setCertificateList(GsonUtils.fromJson(certificateData.getCertificateList().toString(), new TypeToken<List<ResumeBean.CertificateItem>>() {
            }.getType()));
            for (ResumeBean.CertificateItem certificateItems : certificateData.getCertificateList()) {
                certificate += certificateItems.getText() + "\n";
            }
            mDataBinding.etCertificate.setText(certificate);

        } else {
            certificateData = new ResumeBean.CertificateData();
        }
    }

    private void save() {
        String honorary = mDataBinding.etHonorary.getText().toString().trim();
        if (TextUtils.isEmpty(honorary)) {
            ToastUtils.showShort(getString(R.string.please_input_honor_award));
            return;
        }
        String certificate = mDataBinding.etCertificate.getText().toString().trim();
        if (TextUtils.isEmpty(certificate)) {
            ToastUtils.showShort(getString(R.string.please_input_credentials));
            return;
        }

        String[] honoraryLines = honorary.split("\\r?\\n");
        List<ResumeBean.DetailItem> listHonorary = new ArrayList<>();
        for (String line : honoraryLines) {
            listHonorary.add(new ResumeBean.DetailItem(line));
        }

        String[] certificateLines = certificate.split("\\r?\\n");
        List<ResumeBean.CertificateItem> listCertificate = new ArrayList<>();
        for (String line : certificateLines) {
            listCertificate.add(new ResumeBean.CertificateItem(line));
        }

        certificateData.setDetailList(listHonorary);
        certificateData.setCertificateList(listCertificate);

        List<ResumeBean> listSave = ResumeDataUtils.getResumeList();
        module.setData(certificateData);
        listSave.set(position, resumeBean);
        ResumeDataUtils.saveResumeList(listSave);
        ToastUtils.showShort(getString(R.string.save_success));
        finish();
    }

    private void showRenameDialog() {
        TitleRenameDialog renameDialog = new TitleRenameDialog(mContext);
        renameDialog.text = mDataBinding.tvTitle.getText().toString().trim();
        renameDialog.setListener(new TitleRenameDialog.Ilistener() {

            @Override
            public void clickRename(String name) {
                module.setAlias(name);
                mDataBinding.tvTitle.setText(name);
            }
        });
        renameDialog.show();
    }

}